home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3079 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.5 KB  |  138 lines

  1. Path: news.infi.net!usenet
  2. From: nngis@norfolk.infi.net (Greg DiGiorgio)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Finding a prime number
  5. Date: 25 Jan 1996 20:33:00 GMT
  6. Organization: Customer of InfiNet
  7. Message-ID: <4e8pds$4va@nw002.infi.net>
  8. References: <4e875s$nqk@reader2.ix.netcom.com>
  9. Reply-To: nngis@norfolk.infi.net
  10. NNTP-Posting-Host: h-standbyme.norfolk.infi.net
  11. Mime-Version: 1.0
  12. X-Newsreader: WinVN 0.99.3
  13.  
  14. In article <4e875s$nqk@reader2.ix.netcom.com>, advtr@ix.netcom.co says...
  15.  
  16. There's more to it than you are coding. After your code, I have placed a 
  17. sample program to calculate if a number is a prime number and, if not, 
  18. then print out all prime factors of that number. This program does not 
  19. however print out all prime numbers from 1..n. It could be easily 
  20. modified to do that, though.
  21. Hope this helps,
  22. Greg DiGiorgio
  23.  
  24. >
  25. >I need to write a function that will find wether or not a number is
  26. >prime.  I can come close but I get numbers that are not prime with the
  27. >prime numbers.
  28. >Here is the function I wrote.  Any help would be great.  Thanks  Ken(A
  29. >begining C programmer)
  30.  
  31. ... snipped ...
  32.  
  33. /************************************************************************
  34. **
  35.                 PRIME.C
  36.  
  37. *************************************************************************
  38. */
  39.  
  40. #include <stdio.h>
  41.  
  42. int is_prime(int n) {    /* Tell me if a number is a prime number... */
  43.  
  44.     int i;    /* Loop counter */
  45.  
  46.     for (i=2; i<n; i++) {
  47.         if ((n % i)==0)        /* Number, n, is evenly divisible 
  48. */
  49.             return(0);    /* by i. Therefore it can not be 
  50.  */
  51.                     /* be a prime number. So quit and 
  52. */
  53.     }                /* tell caller it is not prime.     
  54.  */
  55.  
  56.     return(1);            /* If we get here, the number is 
  57.  */
  58.                     /* indeed a prime number, so tell 
  59. */
  60. }                       /* caller so.             
  61.  */
  62.  
  63. void calc_prime_factor(int n) { /* Print out all prime factors for a 
  64. number */
  65.  
  66.     int i;    /* Loop counter */
  67.  
  68.     printf("The prime factors of %d are: ",n);  /* First part of 
  69. output */
  70.  
  71.     for (i=2; i<n; i++) {
  72.                     
  73. /*------------------------------*/
  74.         if (is_prime(i))    /* Is 'i' a prime number ?     
  75. */
  76.                     
  77. /*------------------------------*/
  78.             if ((n % i)==0)    /* Is 'n' evenly divisible by     
  79. */
  80.                     /* this prime number? If so,    
  81. */
  82.                     /* we have found a prime 
  83. factor.*/
  84.                     
  85. /*------------------------------*/
  86.                printf("%d and ",i); /* output prime factor  
  87. */
  88.     }
  89.     printf("\n");    /* Last part of output -> newline character.     
  90.  */
  91. }
  92.  
  93. int main (void) {
  94.     int n;
  95.     char c;
  96.  
  97.     /********************************************************
  98.      *   Force user to enter a number between 1 and 100    *
  99.      ********************************************************/
  100. ANOTHER:
  101.     n=0;    /* Force us thru loop at least once */
  102.     while (n<1 || n>100) {
  103.         printf("Enter a positive integer between 1 and 100: ");
  104.         fflush(stdin);
  105.         scanf("%d",&n);
  106.         if (n<1)
  107.             printf("Number is too small.\n");
  108.         else if (n>100)
  109.             printf("Number is too large.\n");
  110.     }
  111.  
  112.     /********************************************************
  113.      * We've gotten the number, now see if the number is    *
  114.      * a PRIME number or calculate its prime factors.    *
  115.      ********************************************************/
  116.      if (is_prime(n))
  117.  
  118.         printf("The number %d is a prime number.\n",n);
  119.  
  120.      else {    /* Not a prime, so calc its prime factors... */
  121.         calc_prime_factor(n);
  122.      }
  123.  
  124.     /********************************************************
  125.      *         Does user want to do it again?        *
  126.      ********************************************************/
  127.     printf("Another number? (Y/N) ");
  128.     fflush(stdin);
  129.     scanf("%c",&c);
  130.     if (c=='Y' || c=='y')
  131.         goto ANOTHER;
  132.     else
  133.         printf("Goodbye!\n");
  134.     return(0);
  135. }
  136.  
  137.  
  138.